package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.primitives.Booleans;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.admin.dto.AdUserDto;
import com.heima.model.admin.pojo.AdUser;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.groovy.util.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.*;

/**
 * @author Ronaldo
 * @date 2021/7/30
 */
@Service
@Transactional
@Slf4j
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    @Autowired
    private AdUserMapper adUserMapper;

    @Override
    public ResponseResult login(AdUserDto adUserDto) {

        //查询用户信息
        AdUser user = this.getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, adUserDto.getName()));

        //判断用户存在与否
        if (Objects.isNull(user)){
            throw new LeadNewsException(AppHttpCodeEnum.NOT_FOUND_USER_INFO);
        }

        //校验密码
        String inputPassword = adUserDto.getPassword();
        String dbPassword = user.getPassword();
        boolean flag = BCrypt.checkpw(inputPassword, dbPassword);
        if (!flag){
            throw new LeadNewsException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        Map<String,Object> resultMap = new HashMap<>(16);
        //获取token
        String token = AppJwtUtil.getToken(user.getId().longValue());
        resultMap.put("token",token);
        user.setPassword("");
        resultMap.put("user",user);

        adUserMapper.updateLoginTime(user.getId());

        return ResponseResult.okResult(resultMap);
    }

    @Override
    public ResponseResult register(AdUser adUser) {
        //判断用户名 手机号 邮箱是否重复
        AdUser checkUsername = this.getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, adUser.getName()));
        if (Objects.nonNull(checkUsername)){
            throw new LeadNewsException(AppHttpCodeEnum.USERNAME_EXISTENCE);
        }

        AdUser checkPhone = this.getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getPhone, adUser.getPhone()));
        if (Objects.nonNull(checkPhone)){
            throw new LeadNewsException(AppHttpCodeEnum.PHONE_EXISTENCE);
        }

        AdUser checkEmail = this.getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getEmail, adUser.getEmail()));
        if (Objects.nonNull(checkEmail)){
            throw new LeadNewsException(AppHttpCodeEnum.EMAIL_EXISTENCE);
        }

        //属性值设置
        String encryptionPassword = BCrypt.hashpw(adUser.getPassword(), BCrypt.gensalt());
        adUser.setPassword(encryptionPassword);
        adUser.setCreatedTime(new Date());
        adUser.setStatus(9);

        this.save(adUser);
        return ResponseResult.okResult("注册成功!!!");
    }
}
